package com.party.admin.biz.system;

import com.party.admin.web.dto.input.system.GroupPermissionInput;
import com.party.common.utils.StringUtils;
import com.party.core.exception.BusinessException;
import com.party.core.model.YesNoStatus;
import com.party.core.model.member.MemberGroupPermission;
import com.party.core.service.member.IMemberGroupPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName MemberGroupPermissionBizService
 * @Description 机构数据权限
 * @Author yifeng
 * @Date 2019/11/26 11:23
 **/
@Service
public class MemberGroupPermissionBizService {
    @Autowired
    private IMemberGroupPermissionService memberGroupPermissionService;

    /**
     * 保存机构权限配置
     *
     * @param permissionInput 输入参数
     * @author yifeng
     * @date 11:38 2019/11/26
     **/
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void save(GroupPermissionInput permissionInput) {
        if (YesNoStatus.NO.getCode().equals(permissionInput.getIsShowAll()) && StringUtils.isEmpty(permissionInput.getTargetIds())) {
            throw new BusinessException(100, "数据id为空");
        }
        memberGroupPermissionService.deleteBySearch(permissionInput.getType(), permissionInput.getMemberId(), permissionInput.getPartnerId());

        if (YesNoStatus.YES.getCode().equals(permissionInput.getIsShowAll())) {
            MemberGroupPermission groupPermission = new MemberGroupPermission();
            groupPermission.setType(permissionInput.getType());
            groupPermission.setPartnerId(permissionInput.getPartnerId());
            groupPermission.setMemberId(permissionInput.getMemberId());
            groupPermission.setIsShowAll(permissionInput.getIsShowAll());
            memberGroupPermissionService.insert(groupPermission);
        } else {
            for (String targetId : permissionInput.getTargetIds().split(",")) {
                MemberGroupPermission groupPermission = new MemberGroupPermission();
                groupPermission.setType(permissionInput.getType());
                groupPermission.setPartnerId(permissionInput.getPartnerId());
                groupPermission.setMemberId(permissionInput.getMemberId());
                groupPermission.setIsShowAll(permissionInput.getIsShowAll());
                groupPermission.setTargetId(targetId);
                memberGroupPermissionService.insert(groupPermission);
            }
        }
    }
}
